<template>
  <!-- 供应商类型 -->
  <div>
    <TopFilter :list="topFilterList" labelWidth="100" :cols="3" :collapse="false" @filterChange="changeHandle"></TopFilter>
    <FilterTable
      ref="table"
      columnsRef="partPartInfoManageUnitTypeIndex"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'rows'"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls">
        <el-button size="small" type="primary" icon="el-icon-plus" @click="addHandle">新增</el-button>
      </template>
    </FilterTable>
    <BaseDialog :visible.sync="visible" :title="dialogTitle" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <BrandModal :type="handleType" :rowData="formData" @close="closeHandler" />
    </BaseDialog>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { queryContCustomer, deleteRowData } from '@/api/part/customerReso';
import BrandModal from './unitModel';
import { confirmAction, notifyAction } from '@/utils';

export default {
  name: 'Brand',
  components: {
    BrandModal
  },
  mixins: [dictionary],
  data() {
    this.conf = { add: '新增', edit: '编辑', show: '显示' };
    return {
      topFilterList: this.createTopFilterList(),
      fetchParams: { xhrAbort: false },
      visible: false,
      dialogTitle: '',
      fetchApi: queryContCustomer,
      columns: this.createTableColumns(),
      handleType: '',
      formData: {}
    };
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: '客户类型名称',
          fieldName: 'customerTypeName'
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 100,
          fixed: 'left',
          render: props => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.editHandle(props.row)}>
                  编辑
                </el-button>
                <el-divider direction="vertical"></el-divider>
                <el-button size="mini" type="text" onClick={() => this.handleDelete(props.row)}>
                  删除
                </el-button>
              </div>
            );
          }
        },
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '客户类型代码',
          dataIndex: 'customerTypeCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '客户类型名称',
          dataIndex: 'customerTypeName',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    addHandle() {
      this.handleType = 'add';
      this.showHandler();
    },
    editHandle(row) {
      this.handleType = 'edit';
      this.formData = row;
      this.showHandler();
    },
    changeHandle(val) {
      this.fetchParams.xhrAbort = true;
      this.fetchParams = Object.assign({}, val, {
        xhrAbort: false
      });
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    showHandler() {
      this.dialogTitle = `${this.conf[this.handleType]}配件往来单位类型`;
      this.visible = true;
    },
    closeHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams.xhrAbort = true;
        this.fetchParams = Object.assign(this.fetchParams, {
          xhrAbort: false
        });
      }
    },
    // 点击删除
    handleDelete(row) {
      // console.log(row);
      confirmAction('此操作将删除此供应商类型，是否继续')
        .then(async confirm => {
          let res = await deleteRowData({
            customerTypeCode: row.customerTypeCode
          });
          if (res.resultCode == 200) {
            notifyAction('删除成功', 'success');
            this.fetchParams.xhrAbort = true;
            this.fetchParams.xhrAbort = false;
          }
        })
        .catch(err => {});
    }
  }
};
</script>

<style lang="less" scoped></style>
